using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using JGSY.CMS.LowCode.Platform.Domain.Entities;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces
{
    /// <summary>
    /// 异常日志应用服务接口
    /// 提供异常日志的业务逻辑操作
    /// </summary>
    public interface IExceptionLogAppService
    {
        /// <summary>
        /// 记录异常日志
        /// </summary>
        /// <param name="exception">异常对象</param>
        /// <param name="traceId">跟踪ID</param>
        /// <param name="correlationId">关联ID</param>
        /// <param name="tenantId">租户ID</param>
        /// <param name="userId">用户ID</param>
        /// <param name="requestPath">请求路径</param>
        /// <param name="httpMethod">HTTP方法</param>
        /// <param name="additionalData">额外数据</param>
        /// <returns>异常日志ID</returns>
        Task<long> LogExceptionAsync(
            Exception exception,
            string? traceId = null,
            string? correlationId = null,
            string? tenantId = null,
            string? userId = null,
            string? requestPath = null,
            string? httpMethod = null,
            Dictionary<string, object>? additionalData = null);

        /// <summary>
        /// 记录严重异常日志
        /// </summary>
        /// <param name="exception">异常对象</param>
        /// <param name="context">异常上下文信息</param>
        /// <returns>异常日志ID</returns>
        Task<long> LogCriticalExceptionAsync(Exception exception, ExceptionContext context);

        /// <summary>
        /// 获取异常日志
        /// </summary>
        /// <param name="id">异常日志ID</param>
        /// <returns>异常日志DTO</returns>
        Task<ExceptionLogDto?> GetExceptionLogAsync(long id);

        /// <summary>
        /// 获取异常日志列表
        /// </summary>
        /// <param name="tenantId">租户ID</param>
        /// <param name="exceptionLevel">异常级别</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>异常日志列表</returns>
        Task<(List<ExceptionLogDto> Items, int TotalCount)> GetExceptionLogsAsync(
            string? tenantId = null,
            string? exceptionLevel = null,
            DateTime? startTime = null,
            DateTime? endTime = null,
            int pageIndex = 1,
            int pageSize = 20);

        /// <summary>
        /// 更新异常修复状态
        /// </summary>
        /// <param name="id">异常日志ID</param>
        /// <param name="isFixed">是否已修复</param>
        /// <param name="fixedBy">修复者</param>
        /// <param name="fixedAt">修复时间</param>
        /// <param name="fixNotes">修复说明</param>
        /// <returns>更新是否成功</returns>
        Task<bool> UpdateFixStatusAsync(long id, bool isFixed, string? fixedBy = null, DateTime? fixedAt = null, string? fixNotes = null);

        /// <summary>
        /// 根据相关ID获取异常日志
        /// </summary>
        /// <param name="correlationId">关联ID</param>
        /// <returns>异常日志列表</returns>
        Task<List<ExceptionLogDto>> GetExceptionLogsByCorrelationIdAsync(string correlationId);

        /// <summary>
        /// 获取异常统计信息
        /// </summary>
        /// <param name="tenantId">租户ID</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>异常统计信息</returns>
        Task<ExceptionStatistics> GetExceptionStatisticsAsync(string? tenantId = null, DateTime? startTime = null, DateTime? endTime = null);
    }

    /// <summary>
    /// 异常上下文信息
    /// </summary>
    public class ExceptionContext
    {
        public string? TraceId { get; set; }
        public string? CorrelationId { get; set; }
        public string? TenantId { get; set; }
        public string? UserId { get; set; }
        public string? UserName { get; set; }
        public string? RequestPath { get; set; }
        public string? HttpMethod { get; set; }
        public string? UserAgent { get; set; }
        public string? IpAddress { get; set; }
        public Dictionary<string, object>? RequestData { get; set; }
        public Dictionary<string, object>? AdditionalData { get; set; }
    }

    /// <summary>
    /// 异常统计信息
    /// </summary>
    public class ExceptionStatistics
    {
        public int TotalExceptions { get; set; }
        public int CriticalExceptions { get; set; }
        public int ErrorExceptions { get; set; }
        public int WarningExceptions { get; set; }
        public int FixedExceptions { get; set; }
        public int UnfixedExceptions { get; set; }
        public Dictionary<string, int> ExceptionTypeCount { get; set; } = new();
        public Dictionary<string, int> ExceptionsByHour { get; set; } = new();
    }
}
